<%-- 
    Document   : page
    Created on : 2014/8/21, 上午 09:27:53
    Author     : gjun
--%>

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.HashMap"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<% 
    //這裡做模糊查詢
    String username = request.getParameter("username");
    String where = "";
    if(username != null && !"".equals(username.trim())){
        where = " where username like '%" + username + "%'";
    }
    
    
    //因資料庫資料龐大，因此需要分頁機制來呈現頁面
    //這裡設定成每頁呈現10筆資料，要做4個前置步驟
    dao.BaseDAO y = new dao.BaseDAO();
    String sql = "select count(*) as total from person" + where;    //取出總筆數並把欄位命名為total
    System.out.println(sql);
    String[] ary = {};
    ArrayList list = y.query(sql, ary);
    
    //1.算資料總筆數
    String total = (String)((HashMap)(list.get(0))).get("total");   
    //2.定義好一頁的筆數
    int pagesize = 10;                                  //一頁10筆                                     
    //3.共有幾頁
    int pages = Integer.parseInt(total) / pagesize;     //總共的頁數            
    if(Integer.parseInt(total) % pagesize != 0){
        pages++;
    }
    //4.如何顯示指定頁碼中的資料，配合sql語法來設定  select * from person limit 從哪一筆開始, 顯示幾筆資料  EX:select * from person limit 0, 10
    String pageNo = request.getParameter("pageNo");                 //使用者要看的頁碼
    if(pageNo == null){
        pageNo = "1";
    }
    int index = (Integer.parseInt(pageNo) - 1) * pagesize;          //select * from person limit index, pagesize
    
    
    //開始執行
    sql = "select * from person "+ where +" limit "+ index + ", "+ pagesize;
    System.out.println(sql);
    //ary = new String[]{index+"", pagesize+""};                                                    //不適合用?，直接sql語法就好
    list = y.query(sql, ary);
    request.setAttribute("list", list);
    
    int prePage = Integer.parseInt(pageNo) == 1 ? 1 : Integer.parseInt(pageNo) - 1;                 //上一頁的號碼
    int nextPage = Integer.parseInt(pageNo) == pages ? pages : Integer.parseInt(pageNo) + 1;        //下一頁的號碼
    %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <script>
            function doPageNo(x){
                document.getElementById('pageNo').value = x;
                document.getElementById('form1').submit();  
                
            }
        </script>
    </head>
    <body>
        
        <form action="page.jsp" id="form1">
            <input type="text" name="username" value="${param.username}">
            <input type="hidden" name="pageNo" id="pageNo" value="">
            <input type="hidden" name="where" id="where" value="">
            <input type="submit">
        </form>
        
        <input type="button" value="上一頁" onclick="doPageNo('<%= prePage %>')">
        <%-- <a href="page.jsp?pageNo=<%= prePage %>">上一頁</a> --%>
        目前頁數<%= pageNo %> / <%= pages %> ， 共<%= total %>筆
        <%-- <a href="page.jsp?pageNo=<%= nextPage %>">下一頁</a> --%>
        <input type="button" value="下一頁" onclick="doPageNo('<%= nextPage %>')">
        <hr>
        
        <c:forEach var="x" items="${list}">
            ${x.id} : ${x.username} : ${x.tel} : ${x.birthday}<br>
        </c:forEach>
          
            
    </body>
</html>
